home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.lib;
-
- import sub_arctic.lib.oneline_text_display;
- import sub_arctic.lib.manager;
- import sub_arctic.output.color_pair;
- import sub_arctic.output.drawable;
- import sub_arctic.input.event;
- import sub_arctic.input.callback_object;
- import sub_arctic.anim.trajectory;
- import sub_arctic.anim.transition;
- import sub_arctic.anim.animatable;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Color;
-
- /**
- * This is a class which draws a shifting portion of a long string on the
- * screen to make "sliding sign". This object has options for putting a box
- * around the text, making it be opaque, setting its colors, and of course
- * controlling the displayed string and position within the string.
- *
- * @author Scott Hudson
- */
- public class sliding_text extends label implements animatable {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Identifier for callback at end of scroll */
- public static int END_OF_SCROLL = 0;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Object we make callbacks to at the end of a scroll */
- protected callback_object _callback_obj = null;
-
- /**
- * Object that we make callbacks to at the end of a scroll. All callbacks
- * have a callback number END_OF_SCROLL and the rest of the fields are
- * unused.
- *
- * @return callback_object the object sends the callbacks to.
- */
- public callback_object callback_obj() {return _callback_obj;}
-
- /**
- * Object that we make callbacks to at the end of a scroll. All callbacks
- * have a callback number END_OF_SCROLL and the rest of the fields are
- * unused.
- */
- public void set_callback_obj(callback_object cb) {_callback_obj = cb;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Make a object using some simple defaults. It doesn't try to position
- * itself. It assumes you'll do this with constraints. It sizes height by
- * the size of the text and width is set to a default.
- *
- * @param String s the string to put in the display
- * @param callback_object cb object to call back at end of scroll
- */
- public sliding_text(String s, callback_object cb) {
- super(s);
- set_w(250);
- set_boxed(true);
- set_autosize(false);
- set_callback_obj(cb);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Again, make an object with some defaults. This time you supply the
- * width. Assumes x and y will use constraints.
- *
- * @param String s the string to display
- * @param int width the width of the displayable area in pixels
- * @param callback_object cb object to call back at end of scroll
- */
- public sliding_text(String s, int width, callback_object cb) {
- super(s,width);
- set_boxed(true);
- set_autosize(false);
- set_callback_obj(cb);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Again, make an object with some defaults. This time you supply the
- * width & a font. Assumes x and y will use constraints.
- *
- * @param String s the displayed string
- * @param int width the number of pixels wide the display area is
- * @param Font f the font render the string in
- * @param callback_object cb object to call back at end of scroll
- */
- public sliding_text(String s, int width, Font f, callback_object cb) {
- super(s,width,f);
- set_boxed(true);
- set_autosize(false);
- set_callback_obj(cb);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Again, make a label with some defaults. This time you supply the
- * font. Assumes x and y will use constraints.
- *
- * @param String s the displayed string
- * @param Font f the font to use for rendering the string
- * @param callback_object cb object to call back at end of scroll
- */
- public sliding_text(String s, Font f, callback_object cb) {
- super(s,f);
- set_w(250);
- set_boxed(true);
- set_autosize(false);
- set_callback_obj(cb);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Current offset from left edge to the first displayed character (which
- * has index first_vis_char within the string).
- */
- protected int x_offset = 0;
-
- /**
- * Shift the text left by the given amount (which must be positive!).
- * @param int dist the amount to shift.
- */
- protected void shift_text_left(int dist)
- {
- x_offset += Math.max(dist,0);
- trim_front_chars();
- damage_self();
- }
-
- /** Index of the first character that is visible under the current offset */
- protected int first_vis_char = 0;
-
- /**
- * Potentially adjust first_vis_char based on new offset. If this moves
- * the index over, it will also move over the offset by the width of the
- * characters removed
- */
- protected void trim_front_chars()
- {
- char ch;
- int ch_w, i;
-
- for (i = first_vis_char; i < text().length(); i++)
- {
- /* measure size of first character */
- ch = text().charAt(i);
- ch_w = _metric.charWidth(ch);
-
- /* if the offset is more than the first character, snip it off */
- if (ch <= x_offset)
- {
- x_offset -= ch_w;
- first_vis_char++;
- }
- else
- {
- /* that character extends past the start of the visible string
- * so we can stop looking. */
- break;
- }
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override to reset offset when text is replaced
- * @param String txt new text to display.
- */
- public void set_text(String txt)
- {
- /* let super class do its thing */
- super.set_text(txt);
-
- /* reset our bookkeeping to put string back at the beginning */
- x_offset = 0;
- first_vis_char = 0;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Return the length of the total display string (in pixels). This is the
- * complete string, not just the portion displayed.
- *
- * @return int length of string.
- */
- public int text_length()
- {
- return _metric.stringWidth(text());
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle start of animation transition. This transition is used to
- * drive calls to shift_text_left() to animate the display.
- *
- * @param transition trans the transition object controlling this.
- * @param trajectory traj the trajectory it is working over.
- * @param double start_t start value along trajectory 0..1.
- * @param Object start_obj first data value out of trajectory (must be
- * a Point object).
- * @param event e event "causing" the animation.
- * @param Object user_info the information associated with then object
- * when the transition was scheduled.
- */
- public void start_transition(
- transition trans, trajectory traj,
- double start_t, Object start_obj,
- event e, Object user_info)
- {
- /* nothing to do here */
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle an animation step. This animation should be along a trajectory of
- * points, and the x value are used to drive calls to shift_text_left().
- *
- * @param transition trans the transition object controlling this.
- * @param trajectory traj the trajectory it is working over.
- * @param double start_t start value of this step (within 0..1 overall)
- * @param Object start_obj start data value for this step (this must be
- * a Point object).
- * @param double end_t end value of this step (within 0..1 overall)
- * @param Object end_obj end data value for this step (this must be
- * a Point object).
- * @param event e event "causing" the animation.
- * @param Object user_info the information associated with then object
- * when the transition was scheduled.
- */
- public void transition_step(
- transition trans, trajectory traj,
- double start_t, Object start_obj,
- double end_t, Object end_obj,
- event e, Object user_info)
- {
- shift_text_left( ((Point)end_obj).x - ((Point)start_obj).x );
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle the end of the animation transition. This animation should be
- * along a trajectory of points, and the x value are used to drive calls to
- * shift_text_left(). The callback END_OF_SCROLL is made at this point.
- *
- * @param transition trans the transition object controlling this.
- * @param trajectory traj the trajectory it is working over.
- * @param double start_t start value of this step (within 0..1 overall)
- * @param Object start_obj start data value for this step (this must be
- * a Point object).
- * @param double end_t end value of this step (within 0..1 overall)
- * @param Object end_obj end data value for this step (this must be
- * a Point object).
- * @param event e event "causing" the animation.
- * @param Object user_info the information associated with then object
- * when the transition was scheduled.
- */
- public void end_transition(
- transition trans, trajectory traj,
- double start_t, Object start_obj,
- double end_t, Object end_obj,
- event e, Object user_info)
- {
- /* do the end scroll callback */
- if (callback_obj() != null)
- callback_obj().callback(this, e, END_OF_SCROLL, null);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Draw the display
- * @param drawable d the drawable on which to do the display
- */
- protected void draw_self_local(drawable d)
- {
- Rectangle clip_rect;
- int draw_index, draw_loc, draw_size;
- int clip_right, str_len;
- String str;
-
- /* clear the background if they asked for that */
- if (opaque())
- {
- d.setColor(draw_colors().background());
- d.fillRect(0,0,w()-1,h()-1);
- }
-
- /* set font to our font, and start drawing in foreground */
- d.setFont(font());
- d.setColor(draw_colors().foreground());
-
- /* draw the text in 10 character chunks starting at first_vis_char until
- * we run off the end of the clipping area
- */
- str_len = text().length();
- draw_index = first_vis_char;
- clip_rect = d.getClipRect();
- clip_right = clip_rect.x + clip_rect.width;
- for (draw_loc = h_spacing()-x_offset;
- draw_loc <= clip_right && draw_index < str_len;
- draw_loc += draw_size)
- {
- /* pull out the chunk of string and draw it */
- str = text().substring(draw_index, Math.min(draw_index+10,str_len));
- d.drawString(str, draw_loc, v_spacing()+_metric.getAscent());
-
- /* set up for the next chunk */
- draw_index += 10;
- draw_size = _metric.stringWidth(str);
- }
-
- /* draw box if requested */
- if (boxed())
- d.drawRect(0,0,w()-1,h()-1);
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-